<?php
namespace Admin\Controller;

class LiveController extends BaseController
{
    public function live_list()
    {
        $Live = M('live');
        $keywords = I('keywords', false);
        if ($keywords) {
            $where = "name like '%$keywords%'";
        }
        $count = $Live->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数

        $res = $Live->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($res as &$v) {
            $v['start_time'] = date('Y-m-d H:s', $v['start_time']);
            $v['danmu'] = 'bmd-' . $v['id'] . '-' . SITE_URL;
            $v['reservation_num'] = M('live_reservation')->where(['live_id' => $v['id']])->count();
            if (!$v['share_qr']) {
                $goodModel = new \WXAPI\Controller\GoodsController();
                $wxapp_qrcode_file_res = $goodModel->getQrcode($v['id'], 0, 'live');
                // dump($wxapp_qrcode_file_res);
                $wxapp_qrcode_file_path = $wxapp_qrcode_file_res['file_path'];
                M('live')->where('id=' . $v['id'])->save(['share_qr' => $wxapp_qrcode_file_path]);
                $v['share_qr'] = $wxapp_qrcode_file_path;
            } else {
                $v['share_qr'] = ltrim($v['share_qr'], '.');
            }
        }

        $show = $Page->show();// 分页显示输出
        $this->assign('page', $show);// 赋值分页输出
        $this->assign('list', $res);
        $this->display();
    }

    public function live()
    {
        $act = I('GET.act', 'add');
        $id = I('GET.id');
        $ad_info = array();
        if ($id) {
            $info = D('live')->where('id=' . $id)->find();
            $info['start_time'] = date('Y-m-d H:i', $info['start_time']);
        }
        $this->assign('info', $info);
        $this->assign('act', $act);
        $this->display();
    }


    public function liveHandle()
    {
        $data = I('post.');
        $data['start_time'] = strtotime($data['start_time']);
        if ($data['act'] == 'add') {

            $r = D('live')->add($data);
        }
        if ($data['act'] == 'edit') {
            $r = D('live')->where('id=' . $data['id'])->save($data);
        }

        if ($data['act'] == 'del') {
            $r = D('live')->where('id=' . $data['del_id'])->delete();
            if ($r) exit(json_encode(1));
        }
        $referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/live/live_List');

        if ($r) {
            $this->success("操作成功", U('Admin/Live/live_list'));
        } else {
            $this->error("操作失败", $referurl);
        }
    }


    public function goods()
    {
        $live_id = I('id');
        if (IS_POST) {
            $data = I('post.');
            if ($data['act'] == 'add') {
                $goods_id = $data['goods_id'];
                $time = time();
                M('live_goods')->add(['live_id' => $data['live_id'], 'goods_id' => $goods_id, 'create_time' => $time]);
                exit(json_encode(['msg' => '添加成功', 'data' => [], 'code' => 1]));
            } elseif ($data['act'] == 'del') {
                $r = D('live_goods')->where('id=' . $data['del_id'])->delete();
                if ($r) exit(json_encode(1));
            }
        }

        $live_goods = M('live_goods a')
            ->field('b.goods_name,b.original_img,a.id,a.goods_id,a.number,a.is_explain')
            ->join('ty_goods b ON a.goods_id = b.goods_id', 'left')
            ->where(['live_id' => $live_id])->order('number asc')->select();
        $this->assign('live_goods', $live_goods);
        $this->assign('live_id', $live_id);
        $this->display();
    }

    public function pushurl()
    {
        $id = I('id');
        $pusher_key = '4T4mskNfd44PUTJp';
        $player_key = '2E3Jwe3AAsofubYt';
        $pusher_url = C('PUSHER_URL');
        $player_url = C('PLAYER_URL');
        $appName = md5(uniqid(microtime(true), true));
        $streamName = md5(uniqid(microtime(true), true));
        $streamName2 = $streamName . '_lhd-v';
        $push_url = push_url($pusher_url, $pusher_key, 3600, $appName, $streamName);
        $player_url = play_url($player_url, $player_key, 3600, $appName, $streamName);

        M('live')->where(['id' => $id])->save(['pusher_url' => $push_url, 'player_url' => $player_url]);
        exit(json_encode(['msg' => '生成成功', 'data' => $push_url, 'code' => 1]));
    }


    public function send_goods_live()
    {
        $platform = 'bmd';
        $live_id = I('live_id');
        $goods_id = I('goods_id');
        $goods = M('goods')->field('goods_id,goods_name,shop_price,goods_remark,original_img,floating,prom_id,store_id')->where(['goods_id' => $goods_id])->find();
        $goods['original_img'] = C('MYOSS')['url'] . $goods['original_img'];
        $goods['shop_price'] = geshi($goods['shop_price']);
        $goods['status_status'] = 0;
        // 商品分类页优惠折扣显示 20181129 亮 START
        if (($goods['prom_id'] > 0) && $goods['prom_id']) {
            $prom_goods = M('prom_goods')->where(array('id' => $goods['prom_id']))->find();
            $prom_code = C('MYOSS')['url'] . $prom_goods['prom_code'];

            $datass = time();
            if (($datass > $prom_goods['start_time']) && ($datass < $prom_goods['end_time'])) {
                $statusss = 1;
                if ($prom_goods['type'] == 0) {
                    $prom_goods_price = geshi($goods['shop_price'] * ($prom_goods['expression'] / 100)); //活动折扣
                    $prom_goods_content = 0;
                } elseif ($prom_goods['type'] == 1) {
                    $prom_goods_price = $goods['shop_price'] - $prom_goods['expression'];
                    $prom_goods_content = 0;
                } elseif ($prom_goods['type'] == 2) {
                    $prom_goods_price = $prom_goods['expression'];
                    $prom_goods_content = 0;
                } elseif ($prom_goods['type'] == 5) {
                    $prom_goods_price = geshi($goods['shop_price'] * ($prom_goods['expression'] / 100)); //活动折扣
                    $prom_goods_content = 0;
                } else {
                    $prom_goods_price = $goods['shop_price'];
                    $prom_goods_content = "购买赠送代金券$prom_goods[expression]元";
                }
                $goods['expression'] = $prom_goods['expression'];
                $goods['expressions'] = huodong_name($prom_goods['type'], $prom_goods['expression']);//'活动！'.str_replace("0","",$prom_goods['expression']).'折';
                $goods['prom_goods_price'] = $prom_goods_price;
                $goods['prom_goods_content'] = $prom_goods_content;
                $goods['prom_code'] = $prom_code;
                $goods['status_status'] = $statusss;

            }
        }

        $url = 'https://iot.vyxjj.com/api/app/live/goods/send';
        $data = ['live_id' => $live_id, 'platform_name' => $platform, 'data' => json_encode($goods)];
        $res = httpRequest($url, 'post', $data);

        $res = json_decode($res, true);
        if ($res && $res['status_code'] == 200) {
            M('live_goods')->where(['live_id' => $live_id])->save(['is_explain' => 0]);
            M('live_goods')->where(['live_id' => $live_id, 'goods_id' => $goods_id])->save(['is_explain' => 1]);
            exit(json_encode(['msg' => '推送成功', 'data' => [], 'code' => 1]));
        } else {
            exit(json_encode(['msg' => '推送失败', 'data' => [], 'code' => 0]));
        }


    }

}